package com.tangqiao;

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.MouseEvent;

public class Clicker {
	private Point base;
	private Robot robot;
	public Clicker(Point base) {
		this.base = base;
		try {
			robot = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}
	
	public Clicker(int x, int y) {
		this(new Point(x, y));
	}
	
	public void click(String moves) {
		int x1, y1, x2, y2;
		if (moves.length() != 4) return ;
		x1 = moves.charAt(0) - 'a';
		y1 = 9 - (moves.charAt(1) - '0');
		
		x2 = moves.charAt(2) - 'a';
		y2 = 9 - (moves.charAt(3) - '0');
		Point from = new Point(base);
		from.x = base.x + x1*48;
		from.y = base.y + y1*48;
		Point to = new Point(base);
		to.x = base.x + x2*48;
		to.y = base.y + y2*48;
		try {
			Thread.sleep(50);
			robot.mouseMove(from.x , from.y);
			Thread.sleep(50);
			robot.mousePress(MouseEvent.BUTTON1_MASK);
			Thread.sleep(50);
			robot.mouseRelease(MouseEvent.BUTTON1_MASK);
			Thread.sleep(500);
			robot.mouseMove(to.x , to.y);
			Thread.sleep(50);
			robot.mousePress(MouseEvent.BUTTON1_MASK);
			Thread.sleep(50);
			robot.mouseRelease(MouseEvent.BUTTON1_MASK);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
}
